"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dbcompatibility-whale
Case Name   : 函数gms_output.enable()启用gms_output软件包功能，超范围参数有提示信息
Create At   : 2023/12/13
Owner       : @zou_jialiang050
Description :
    1.函数select gms_output.enable();
    2.函数select gms_output.enable(1500000);超范围
Expect      :
    1.成功
    2.成功
History     :
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Whale(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbname = 'db_whale_33'
    
    def test_gms_output(self):
        text = "---pre1:创建A兼容性数据库;expect:成功---"
        self.logger.info(text)
        pre_cmd1 = f"drop database if exists {self.dbname};" \
            f"create database {self.dbname} dbcompatibility='A';"
        pre_msg1 = self.sh_primy.execut_db_sql(pre_cmd1)
        self.logger.info(pre_msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, pre_msg1,
                      text + '执行失败')
        text = "---pre2:在A数据库中创建whale插件;expect:成功---"
        self.logger.info(text)
        pre_cmd2 = f"create extension whale;"
        pre_msg2 = self.sh_primy.execut_db_sql(pre_cmd2, dbname=self.dbname)
        self.logger.info(pre_msg2)
        self.assertIn(self.constant.extension_create_success, pre_msg2,
                      text + '执行失败')
        
        text = "---step1:启用gms_output软件包功能;expect:成功---"
        self.logger.info(text)
        sql_cmd1 = f"select gms_output.enable();"
        msg1 = self.sh_primy.execut_db_sql(sql_cmd1, dbname=self.dbname)
        self.logger.info(msg1)
        self.common.equal_sql_mdg(msg1, 'enable', '', '(1 row)', flag='1')

        text = "---step2:启用gms_output软件包功能，缓冲区大小超范围;expect:成功---"
        self.logger.info(text)
        sql_cmd2 = f"select gms_output.enable(1500000);"
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.dbname)
        self.logger.info(msg2)
        self.assertIn('WARNING:  Limit decreased to 1000000 bytes', msg2,
                      text + '执行失败')

        text = "---step3:启用gms_output软件包功能，缓冲区大小在范围内;expect:成功---"
        self.logger.info(text)
        sql_cmd3 = f"select gms_output.enable(1000000);"
        msg3 = self.sh_primy.execut_db_sql(sql_cmd3, dbname=self.dbname)
        self.logger.info(msg3)
        self.common.equal_sql_mdg(msg3, 'enable', '', '(1 row)', flag='1')
    
    def tearDown(self):
        text = '----清理环境----'
        self.logger.info(text)
        clean_cmd = f"drop database {self.dbname};"
        clean_msg = self.sh_primy.execut_db_sql(clean_cmd)
        self.logger.info(clean_msg)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_msg,
                      text + '执行失败')
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
